package com.ssy.lingxi.order.serviceimpl.base;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.order.entity.OrderDO;
import com.ssy.lingxi.order.model.bo.VendorBO;
import com.ssy.lingxi.order.model.dto.OrderMemberDTO;
import com.ssy.lingxi.order.model.dto.OrderSalesDTO;
import com.ssy.lingxi.order.service.base.IBaseOrderAuthService;
import com.ssy.lingxi.order.service.base.IBaseOrderService;
import com.ssy.lingxi.order.service.feign.IMemberFeignService;
import com.ssy.lingxi.order.utils.NumberUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础订单权限相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2022-01-06
 */
@Service
public class BaseOrderAuthServiceImpl implements IBaseOrderAuthService {
    @Resource
    private IBaseOrderService baseOrderService;

    @Resource
    private IMemberFeignService memberFeignService;

    /**
     * 更新供应商用户Id字段，要在流程执行之后调用此接口，调用方要保存OrderDO
     *
     * @param order 订单
     */
    @Override
    public void updateVendorUserId(OrderDO order) {
        order.setVendorUserId(baseOrderService.hasSubmittedToVendor(order) ? findVendorSales(order.getBuyerMemberId(), order.getBuyerRoleId(), order.getVendorMemberId(), order.getVendorRoleId()) : 0L);
    }

    /**
     * 更新供应商用户Id字段，要在流程执行之后调用此接口，调用方要保存OrderDO
     *
     * @param order 订单
     * @param sales 供应商业务员信息列表
     */
    @Override
    public void updateVendorUserId(OrderDO order, List<OrderSalesDTO> sales) {
        order.setVendorUserId(baseOrderService.hasSubmittedToVendor(order) ? sales.stream().filter(s -> s.getBuyerMemberId().equals(order.getBuyerMemberId()) && s.getBuyerRoleId().equals(order.getBuyerRoleId()) && s.getVendorMemberId().equals(order.getVendorMemberId()) && s.getVendorRoleId().equals(order.getVendorRoleId())).map(OrderSalesDTO::getVendorUserId).findFirst().orElse(0L) : 0L);
    }

    /**
     * 更新供应商用户Id字段，要在流程执行之后调用此接口，调用方要保存OrderDO
     *
     * @param order        订单
     * @param vendorUserId 供应商业务员用户Id
     */
    @Override
    public void updateVendorUserId(OrderDO order, Long vendorUserId) {
        order.setVendorUserId(baseOrderService.hasSubmittedToVendor(order) ? vendorUserId : 0L);
    }

    /**
     * 从会员服务查询供应商业务员Id
     *
     * @param buyerMemberId  采购会员Id
     * @param buyerRoleId    采购会员角色Id
     * @param vendorMemberId 供应会员Id
     * @param vendorRoleId   供应会员角色Id
     * @return 供应会员业务员用户Id，如没有返回0
     */
    @Override
    public Wrapper<Long> findVendorSalesUserId(Long buyerMemberId, Long buyerRoleId, Long vendorMemberId, Long vendorRoleId) {
        return memberFeignService.findVendorSalesUserId(buyerMemberId, buyerRoleId, vendorMemberId, vendorRoleId);
    }

    /**
     * 从会员服务查询供应商业务员Id
     *
     * @param buyerMemberId  采购会员Id
     * @param buyerRoleId    采购会员角色Id
     * @param vendorMemberId 供应会员Id
     * @param vendorRoleId   供应会员角色Id
     * @return 供应会员业务员用户Id，如没有返回0
     */
    @Override
    public Long findVendorSales(Long buyerMemberId, Long buyerRoleId, Long vendorMemberId, Long vendorRoleId) {
        try {
            Wrapper<Long> salesResult = memberFeignService.findVendorSalesUserId(buyerMemberId, buyerRoleId, vendorMemberId, vendorRoleId);
            return NumberUtil.isNullOrZero(salesResult.getData()) ? 0L : salesResult.getData();
        } catch (Exception ignored) {
            return 0L;
        }
    }


    /**
     * 批量查询供应商业务员用户Id
     *
     * @param buyerMemberId 采购会员id
     * @param buyerRoleId   采购会员角色Id
     * @param vendors       供应商列表
     * @return 查询结果
     */
    @Override
    public Wrapper<List<OrderSalesDTO>> findVendorSales(Long buyerMemberId, Long buyerRoleId, List<VendorBO> vendors) {
        return memberFeignService.findVendorSales(buyerMemberId, buyerRoleId, vendors);
    }

    /**
     * 批量查询供应商业务员用户Id
     *
     * @param members 采购会员与供应会员列表
     * @return 查询结果
     */
    @Override
    public Wrapper<List<OrderSalesDTO>> findVendorSales(List<OrderMemberDTO> members) {
        return memberFeignService.findVendorSales(members);
    }
}
